/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

/**
 *
 * @author ceci torres
 */
import java.sql.*;

public class Dao {

    public Connection conexion;
    public final static String userDb = "root";
    public final static String passDb = "jellybeans009";

    //Conectar a la Base de datos
    public void conectar() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb1", userDb, passDb);
    }
    //Desconectar a la Base de datos

    public void desconectar() throws SQLException, ClassNotFoundException {
        conexion.close();
    }

    //Metodo para consultar si un email y contraseñan pertenecen a una cuenta registrada
    public boolean isAcountExists(String email, String password) throws SQLException {
        String sql = "SELECT * FROM usuarios WHERE email='" + email + "' AND password='" + password + "'";
        PreparedStatement ps = conexion.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        return rs.next();
    }

    //Metodo para consultar si el email recibido ya esta registrado
    public boolean isEmailRegistered(String email) throws SQLException {
        String sql = "SELECT * FROM usuarios WHERE email='" + email + "'";
        PreparedStatement ps = conexion.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        return rs.next();
    }

    //Metodo para registrar una cuenta
    public void registerUser(String email, String password, String name) throws SQLException {
        String sql = "INSERT INTO `usuarios`(`email`,`password`,`name`) VALUES ('" + email + "','" + password + "','" + name + "')";
        PreparedStatement ps = conexion.prepareStatement(sql);
        ps.executeUpdate();
    }
}
